﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

public partial class Modules_Search : System.Web.UI.UserControl
{
    private  static string filetype="";//全局变量，文件类型
    /********* void ShowResult(DataTable list) ****************
     * 绑定搜索后的 DataTable 到 结果列表显示控件
    ***********************************************************/
    private void ShowResult(DataTable list)
    {
        Result_Count.Text = "";
        this.GridView_Result_List.DataSource = null;
        this.GridView_Result_List.DataBind();
        Result_Count.Text = " 共搜索到 " + list.Rows.Count.ToString() + "  条记录<br /><br />";
        this.GridView_Result_List.DataSource = list;
        this.GridView_Result_List.DataBind();
    }
    /********* void SearchByType(string typetemp) *************
     * 根据文件类型进行查询，即在点击左边的文件类型按钮之后对
     * 列表进行刷新
    ***********************************************************/
    private void SearchByType(string typetemp)
    {
        string strsql;
        if (typetemp == "")
            strsql = "SELECT * FROM resource";
        else
            strsql = "SELECT * FROM resource WHERE filetype =" + typetemp;
        DataTable ResultList = new DataTable();
        MysqlHelp ResultListSQL = new MysqlHelp();
        ResultList = ResultListSQL.GetDatabysql(strsql);
        ShowResult(ResultList);
    }
    /********* void CommonSearch() *****************************
     * 普通搜索，即只需输入一个关键字（非数据库中的关键字，而是用
     * 户所需要的搜索项），即按照该关键字与当前的文件类型进行搜索
    ***********************************************************/
    private void CommonSearch()
    {
        string type;
        if (ViewState["filetype"] == null)
            type = "";
        else
             type = ViewState["filetype"].ToString();
        string keyword = Common_TextBox_Title.Text;
        string strsql;
        if (type == "")
            strsql = "SELECT * FROM resource WHERE CONCAT(title,keyword,author,source,introduction) LIKE '%" + keyword + "%'";
        else
            strsql = "SELECT * FROM resource WHERE CONCAT(title,keyword,author,source,introduction) LIKE '%" + keyword + "%'" + "AND filetype = " + type;
        DataTable ResultList = new DataTable();
        MysqlHelp ResultListSQL = new MysqlHelp();
        ResultList = ResultListSQL.GetDatabysql(strsql);
    
        ShowResult(ResultList);
    }
    /********* void InitDropDownList() ************************
     * 初始化精确搜索中的下拉列表，包括 学科以及四个时间，文件
     * 类型下拉列表的选项为静态的，无需动态增添
     **********************************************************/
    private void InitDropDownList()
    {
        //初始化学科下拉菜单
        this.Exact_DropDownList_Subject.Items.Clear();
        DataTable SubList = new DataTable();
        MysqlHelp SubListSQL = new MysqlHelp();
        SubList = SubListSQL.GetDatabysql("SELECT * FROM subject");
        int count = SubList.Rows.Count;
        for(int i = 0;i<count;i++)
        {
            string sub = SubList.Rows[i][1].ToString();
            this.Exact_DropDownList_Subject.Items.Add(new ListItem(sub,sub));
        }
        //初始化更新时间处的下拉列表，年度为2010至今
        int nowyear = Convert.ToInt32(System.DateTime.Now.Year);
        this.Year_From.Items.Clear(); 
        this.Year_To.Items.Clear();
        this.Month_From.Items.Clear();
        this.Month_To.Items.Clear();
        for (int i =2010; i <= nowyear; i++)
        {
            this.Year_From.Items.Add(new ListItem(i.ToString(), i.ToString()));
            this.Year_To.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        for(int i=1;i<13;i++)
        {
            this.Month_From.Items.Add(new ListItem(i.ToString(), i.ToString()));
            this.Month_To.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        SearchByType("");
    }
    protected void Button_Serch_Exact_Click(object sender, EventArgs e)
    {
        InitDropDownList();
        this.MultiView_Search.SetActiveView(View_Search_Exact);
        this.MultiView_Search_Result.SetActiveView(View_Result_Detail);
    }
    protected void Button_Serch_Common_Click(object sender, EventArgs e)
    {
        MultiView_Search.SetActiveView(View_Search_Common);
    }
    protected void Menu_Button_AllType_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "";
        SearchByType("");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Text_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "1";
        SearchByType("1");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Voice_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "2";
        SearchByType("2");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Movie_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "3";
        SearchByType("3");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Image_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "4";
        SearchByType("4");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Flash_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "5";
        SearchByType("5");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    protected void Menu_Button_Course_Click(object sender, EventArgs e)
    {
        ViewState["filetype"] = "6";
        SearchByType("6");
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
    /***void Common_Button_Search_Click(object sender, EventArgs e) **
     * 普通查询，按钮点击事件
    **********************************************************/
    protected void Common_Button_Search_Click(object sender, EventArgs e)
    {
        CommonSearch();
    }
    /*** void Exact_Button_Search_Click(object sender, EventArgs e) **
    * 精确查询，按钮点击事件
    **********************************************************/
    protected void Exact_Button_Search_Click(object sender, EventArgs e)
    {
        string title = this.Exact_TextBox_Title.Text == "" ? "IS NOT NULL" : ("LIKE '%" + this.Exact_TextBox_Title.Text+"%'");
        string keyword = this.Exact_TextBox_KeyWord.Text == "" ? "IS NOT NULL" : ("LIKE '%" + this.Exact_TextBox_KeyWord.Text + "%'");
        string source = this.Exact_TextBox_Resource.Text == "" ? "IS NOT NULL" : ("LIKE '%" + this.Exact_TextBox_Resource.Text + "%'");
        string author = this.Exact_TextBox_Author.Text == "" ? "IS NOT NULL" : ("LIKE '%" + this.Exact_TextBox_Author.Text + "%'");
        string uploder = this.Exact_TextBox_Uploader.Text == "" ? "IS NOT NULL" : ("LIKE '%" + this.Exact_TextBox_Uploader.Text + "%'");
        string subject = "= '"+this.Exact_DropDownList_Subject.SelectedValue+"'";
        string type = this.Exact_DropDownList_Type.SelectedValue;
        string timefrom = this.Year_From.SelectedValue + "-" + this.Month_From.SelectedValue + "-1";
        string timeto = this.Year_To.SelectedValue + "-" + this.Month_To.SelectedValue + "-1";

        string strSQL = "SELECT * FROM resource WHERE title " +
                        title+" AND keyword " + keyword +
                        " AND source " + source +
                        " AND author " + author + " AND publisherID " + uploder +
                        " AND subject" + subject + 
                        " AND filetype = " + type +
                        " AND updatatime BETWEEN '" + timefrom + "' AND '" + timeto + "'";
        DataTable ResultList = new DataTable();
        MysqlHelp ResultListSQL = new MysqlHelp();
        ResultList = ResultListSQL.GetDatabysql(strSQL);
        ShowResult(ResultList);
        this.MultiView_Search.SetActiveView(View_Search_Common);
        this.MultiView_Search_Result.SetActiveView(View_Result_List);
    }
}
